<?php
/**
 * SweetPlurk
 *
 * @package    SweetPlurk
 * @copyright  Copyright (c) 2008-2012 Wabow Information Inc. (http://www.wabow.com)
 * @license    New BSD License
 * @version    $Id$
 */

/**
 * Definition
 */
if (!defined('SWEETPLURK_PATH')) {
    define('SWEETPLURK_PATH', dirname(__FILE__));
}

/**
 * Load all files
 */


/**
 * SweetPlurk
 *
 * @package    SweetPlurk
 * @copyright  Copyright (c) 2008-2012 Wabow Information Inc. (http://www.wabow.com)
 * @license    New BSD License
 */
class SweetPlurk_Go
{
    /**
     * Connector
     *
     * @var SweetPlurk_Connector
     */
    protected static $_connector = null;

    /**
     * 資訊
     *
     * @var object
     */
    protected $_data = null;

    /**
     * 建構
     *
     * @param object $data
     */
    protected function __construct($data = null)
    {
        $this->_data = (object) $data;
    }

    /**
     * 取得值
     *
     * @param string $name
     * @return mixed
     */
    public function __get($name)
    {
        if (isset($this->_data->{$name})) {
            return $this->_data->{$name};
        }
    }

    /**
     * 設定 API Key
     *
     * @param string $apiKey
     * @return SweetPlurk_Go
     */
    public static function init($options)
    {
        self::_autoload();

        $connectorClass = 'SweetPlurk_Connector_Curl';
        if (isset($options['connectorClass'])) {
            $connectorClass = $options['connectorClass'];
        }
        if (isset($options['connectorType'])) {
            $connectorType = ucfirst(strtolower($options['connectorType']));
            $connectorClass = 'SweetPlurk_Connector_' . $connectorType;
        }

        if (is_subclass_of($connectorClass, 'SweetPlurk_Connector')) {
            self::$_connector = new $connectorClass($options);
        } else {
            die("Class '$connectorClass' is not sub class of 'SweetPlurk_Connector'");
        }

        return new SweetPlurk_Go();
    }

    /**
     * 登入
     *
     * @param string $username
     * @param string $password
     * @return SweetPlurk_Profile
     */
    public function login($username, $password)
    {
        self::$_connector->resetCookie();
        $userData = self::$_connector->send('/Users/login', array(
            'username' => $username,
            'password' => $password,
        ));
        return new SweetPlurk_Profile($userData);
    }

    /**
     * 取得公開 Profile
     *
     * @param string $useranme
     * @return SweetPlurk_Profile
     */
    public function getPublicProfile($useranme)
    {
        $profileData = self::$_connector->send('/Profile/getPublicProfile', array(
            'user_id' => $username,
        ));
        return new SweetPlurk_Profile($profileData);
    }

    /**
     * 設定自動載入
     *
     */
    protected static function _autoload()
    {
        self::_autoloadDir(dirname(__FILE__));
    }

    /**
     * 載入目錄下所有 PHP 檔案
     *
     * @param string $dir
     * @param bool $topLevel
     */
    protected static function _autoloadDir($dir, $topLevel = true)
    {
        static $dirList = array();
        $iterator = new DirectoryIterator($dir);
        foreach ($iterator as $entry) {
            if (!preg_match('/^\./', $entry)) { // 避開 ".", "..", ".svn" 等不必要的目錄
                $entry = rtrim($dir, '/') . '/' . $entry;
                if (is_dir($entry) && !isset($dirList[$dir])) {
                    $dirList[$dir] = $entry;
                } elseif (is_file($entry) && preg_match('/\.php$/', $entry) && is_readable($entry)) {
                    require_once $entry;
                }
            }
        }

        // 依序載入子目錄
        if ($topLevel) {
            foreach ($dirList as $dir) {
            	self::_autoloadDir($dir, false);
            }
        }
    }
}